#define _CRT_SECURE_NO_WARNINGS  1


class Solution {
public:
    int Sum(int i)
    {
        int sum = 0;
        while (i)
        {
            int j = i % 10;
            sum += j * j;
            i = i / 10;
        }
        return sum;
    }

    bool isHappy(int n)
    {
        int slow = n;
        int fast = Sum(n);
        while (slow != fast)
        {
            slow = Sum(slow);
            fast = Sum(Sum(fast));
        }
        return slow == 1;
    }
};